﻿using Models;
using System;
using System.Collections.ObjectModel;
using System.Reflection;
using System.Windows.Controls;
using System.Windows.Input;

namespace MainClient2
{
    internal class MainWindowViewModel : BindableBase
    {
        public ObservableCollection<SmartClientMenu> Menus { get; set; } = new ObservableCollection<SmartClientMenu>() { };

        public SmartClientMenu SelectMenu { get; set; }
        public SmartClientMenu SelectSubMenu { get; set; }
        public UserControl CurrentUserControl { get; set; }

        public MainWindowViewModel()
        {
            Constant.Menus.ForEach(x => Menus.Add(x));
        }

        public ICommand ChangeMenuCommand
        {
            get
            {
                return new RelayCommand((para) =>
                {
                    if (SelectSubMenu == null)
                    {
                        return;
                    }
                    if (SelectSubMenu.MenuUserControl == null)
                    {
                        Assembly assembly = Assembly.Load(SelectSubMenu.AssemblyName);//1 动态加载
                        if (assembly == null)
                        {
                            return;
                        }
                        Type dbHelperType = assembly.GetType(SelectSubMenu.MenuControlName);//2 获取类型
                        if (dbHelperType == null)
                        {
                            return;
                        }
                        object oDBHelper = Activator.CreateInstance(dbHelperType);//3 创建对象
                        if (oDBHelper == null)
                        {
                            return;
                        }
                        UserControl dbHelper = (UserControl)oDBHelper;//4 接口强制转换
                        if (dbHelper == null)
                        {
                            return;
                        }
                        SelectSubMenu.MenuUserControl = dbHelper;
                    }
                    CurrentUserControl = (UserControl)SelectSubMenu.MenuUserControl;
                    //SelectSubMenu.MenuControl
                });
            }
        }
    }
}
